package com.example.common.handler;

import com.example.common.api.IErrorCode;
import com.example.common.enums.ResponseCode;
import lombok.Data;

/**
 * @author mie
 */
@Data
public class BusinessException extends RuntimeException implements IErrorCode {
    private Integer code;
    private String msg;



    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
        this.msg = message;
    }

    /**
     * 创建一个自定义异常
     *
     * @param code    异常码
     * @param message 异常信息
     * @return BusinessException
     */
    public static BusinessException createException(Integer code, String message) {
        return new BusinessException(code, message);
    }

    /**
     * 创建一个成功自定义异常
     *
     * @param message 异常信息
     * @return BusinessException
     */
    public static BusinessException createSuccessException(String message) {
        return new BusinessException(ResponseCode.SUCCESS.getCode(), message);
    }

    /**
     * 创建一个错误自定义异常
     *
     * @param message 异常信息
     * @return BusinessException
     */
    public static BusinessException createErrorException(String message) {
        return new BusinessException(ResponseCode.FAILED.getCode(), message);
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return msg;
    }
}